<template>
  <el-card shadow="always">
    <el-form :inline="true" :model="empSearch" class="demo-form-inline" size="small">
      <el-form-item >
        <el-input v-model="empSearch.ename" placeholder="员工姓名"></el-input>
      </el-form-item>
      <el-form-item >
        <el-input v-model="empSearch.city" placeholder="所在城市"></el-input>
      </el-form-item>
      <el-form-item >
        <el-input v-model="empSearch.startSalary" placeholder="最低工资"></el-input>
      </el-form-item>
      <el-form-item >
        至
      </el-form-item>
      <el-form-item >
        <el-input v-model="empSearch.endSalary" placeholder="最高工资"></el-input>
      </el-form-item>
      <el-form-item >
        <el-select v-model="empSearch.did" placeholder="所在部门">
          <el-option label="所有部门" value="0"></el-option>
          <el-option :label="dept.dname" :value="dept.did" v-for="dept in deptList" :key="dept.did"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search" icon="el-icon-search" plain>查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="add" icon="el-icon-plus" plain>添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="handleDelete" icon="el-icon-delete" plain>批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      stripe
      :data="emps"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="员工姓名"
        prop="ename"
        align="center"/>
      <el-table-column
        label="出生日期"
        align="center">
        <template slot-scope="scope">
          {{toDateStr(scope.row.bdate)}}
        </template>
      </el-table-column>
      <el-table-column
        label="性别"
        align="center">
        <template slot-scope="scope">
          {{scope.row.sex == 'M' ? "男" : "女"}}
        </template>
      </el-table-column>
      <el-table-column
        label="所在城市"
        prop="city"
        align="center"/>
      <el-table-column
        label="所在部门"
        prop="dname"
        align="center"/>
      <el-table-column
        label="入职时间"
        align="center">
        <template slot-scope="scope">
          {{toDateStr(scope.row.startdate)}}
        </template>
      </el-table-column>
      <el-table-column
        label="工资"
        prop="salary"
        align="center"/>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              size="mini"
              type="primary" icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.$index, scope.row)"></el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      align="left"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="page"
      :page-size="pageSize"
      layout="prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </el-card>
</template>
<script>
  import empApi from '@/api/emp'
  import deptApi from '@/api/dept'
  export default {
    data() {
      return {
        empSearch:{},
        emps: [],   // 所有记录的结果
        deptList:[], // 部门列表
        page:1,
        pageSize:3,
        total:100,
        multipleSelection: [],
        eids:[],     // 代表要删除的员工编号
      }
    },
    created() {
      this.getDeptList()
      this.search()
    },
    // 利用计算属性完成日期的格式化处理
    computed:{
      toDateStr(){
        return (dt) => {
          let dt1 = new Date(dt)
          return dt1.getFullYear() + "年" + (dt1.getMonth() + 1) + "月" + dt1.getDate() + "日"
        }
      }
    },
    methods: {
      handleSelectionChange(val) {
        this.multipleSelection = val;
        console.log("this.multipleSelection",this.multipleSelection)
      },
      add(){
        this.$router.push("/emp/edit")
      },
      //1. 查询所有的部门列表
      getDeptList(){
        deptApi.getDeptList().then(({data}) => {
          this.deptList = data.list
        })
      },
      //1. 条件查询带分页
      search(){
        empApi.search(this.page,this.pageSize,this.empSearch).then(({data}) => {
          console.log("data:",data)
          this.emps = data.pageModel.list
          this.total = data.pageModel.total
        })
      },
      // 1. 编辑员工信息
      handleEdit(index, row) {
        // 1.1 跳转到指定的路由地址
        console.log("row:",row)
        this.$router.push(`/emp/edit?eid=`+row.eid)
      },
      // 2. 删除学员信息
      handleDelete(index, row) {
        console.log(row)
        this.eids = []
         if(row){   // 此时单个删除
            this.eids.push(row.eid)
         }else{    // 如果是批量删除
            this.multipleSelection.map(m => {
              this.eids.push(m.eid)
            })
         }
        // 从后台删除
        this.$confirm(`是否真的要删除员工编号为：${this.eids}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          empApi.removeEmp(this.eids).then((data)=>{
            console.log("data:",data)
            this.$message.success(data.message)
            this.page = 1
            this.search()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      // 页大小pageSize发生改变时触发
      handleSizeChange(pageSize) {
        this.pageSize = pageSize;
        this.search()
        console.log(`每页 ${pageSize} 条`);
      },
      // 页page改变时触发
      handleCurrentChange(page) {
        this.page = page;
        this.search()
        console.log(`当前页: ${page}`);
      }
    }
  }
</script>
<style scoped>
  .el-pagination{
    margin-top: 20px;
  }
</style>
